// pages/product/goodsDetail/goodsDetail.js
import wxCharts from '../../../utils/wx-charts'
import {getGoodsDetail,getGoodsOffer} from '../../../network/product'
import moment from '../../../utils/moment'
import { toast } from '../../../utils/Tools';
Page({
  data: {
    produce_id:null,
    isOffer: false,
    isParameter: false,
    week: [
      { text: '一月', value: 0 },
      { text: '一周', value: 1 }
    ],
    ticket: [
      { text: '含票', value:0 },
      { text: '不含票', value: 1 }
    ],
    offerSele:{
      week: 0, ticket: 0
    },
    detailinfo:{},
    offerInfo:{},
    categories:[],
    imageWidth:null
  },
  onLoad: function (options) {
    const imageWidth = wx.getSystemInfoSync().windowWidth / 375;
    this.setData({
      imageWidth
    });
    this.setData({produce_id:options.product_id})
    this._getGoodsDetail(options.product_id)
    this._getGoodsOffer(options.product_id)
  },
  //      函数事件    ///
  clickDownMenu(){
    this.setData({
      imageWidth:0
    })
  },
  closeDownMenu(){
    this.setData({
      imageWidth:1
    })
  },
  handleDownMenu(e){  //  处理选择下拉菜单
    const {type} = e.currentTarget.dataset
    const imageWidth = wx.getSystemInfoSync().windowWidth / 375;
    this.setData({
      [`offerSele.${type}`]:e.detail,
    })
    this._getGoodsOffer(this.data.produce_id)
    setTimeout(()=>{
      this.setData({
        imageWidth
      })
      this._wxCharts()
    },500)
  },
  handleSheet(e) {
    switch (e.currentTarget.dataset.sheet) {
      case 'offer':
        this._wxCharts()
        this.setData({
          isOffer: true
        })
        break;
      case 'parameter':
        this.setData({
          isParameter: true
        })
        break;
    }
  },
  forbidScroll(e) {
    return;
  },
  onClose() {
    this.setData({ isOffer: false ,isParameter:false });
  },
  _wxCharts(){
    if(this.data.categories.length){
      new wxCharts({
        canvasId: 'offer',
        type: 'line',
        categories: this.data.categories,
        series: this.data.offerInfo.series,
        yAxis: this.data.offerInfo.yAxis,
        width: (375 * this.data.imageWidth),
        height: (250 * this.data.imageWidth),
        dataLabel: false
      });
    }else{
      toast('没有参考报价数据')
      setTimeout(()=>{
        this.setData({ isOffer: false });
      },1000)
    }
  },
  //      网络请求    ///
  async _getGoodsDetail(produce_id){
    const res = await getGoodsDetail(produce_id)
    this.setData({
      detailinfo:res.data
    })
  },
  _getGoodsOffer(product_id){
    const data={
      week: this.data.offerSele.week,
      ticket: this.data.offerSele.ticket,
      product_id
    }
    getGoodsOffer(data).then(res=>{
      if(res.code === 200){
        this.setData({offerInfo:res.data})
        let handleCate = [];
        this.data.offerInfo.categories.forEach(res=>{
          handleCate.push(moment(res*1000,'DD')+"日")        
        })
        this.setData({
          categories:handleCate
        })
      }
    })
  }
})